package io.renren.modules.sq.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.RandomUtils;
import io.renren.common.utils.ResponseData;
import io.renren.modules.sq.dto.SysAuthDto;
import io.renren.modules.sq.entity.SqSystemAuthorizationCodeEntity;
import io.renren.modules.sq.entity.SqSystemEditionEntity;
import io.renren.modules.sq.service.SqSystemAuthorizationCodeService;
import io.renren.modules.sq.service.SqSystemEditionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.sq.entity.SqSystemEntity;
import io.renren.modules.sq.service.SqSystemService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.sound.midi.Soundbank;


/**
 * 系统表
 *
 * @author txw_cyl
 * @email sunlightcs@gmail.com
 * @date 2022-05-14 19:29:39
 */
@Slf4j
@Api(tags = "系统")
@RestController
@RequestMapping("sys/sqsystem")
public class SqSystemController {
    @Autowired
    private SqSystemService sqSystemService;
    @Autowired
    private SqSystemEditionService sqSystemEditionService;
    @Autowired
    private SqSystemAuthorizationCodeService sqSystemAuthorizationCodeService;

    /**
     * 列表
     */
    @ApiOperation("获取系统列表")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页pageIndex", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "limit", value = "每页数量pageSize", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "systemName", value = "系统名称", paramType = "query", dataType = "String", required = false),
            // @ApiImplicitParam(name = "systemEdition", value = "系统版本", paramType = "query", dataType = "String", required = false),
            // @ApiImplicitParam(name = "systemDescription", value = "描述", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "allow", value = "允许生成授权码0：禁止1：允许", paramType = "query", dataType = "String", required = false),
    })
    public ResponseData<Page<SqSystemEntity>> list(@RequestParam Map<String, Object> params) {
        Page<SqSystemEntity> page = sqSystemService.getSysList(params);
        for (SqSystemEntity record : page.getRecords()) {
            List<SqSystemEditionEntity> list = sqSystemEditionService.list(new QueryWrapper<SqSystemEditionEntity>().eq("system_id", record.getId()));
            for (SqSystemEditionEntity sqSystemEdition : list) {
                sqSystemEdition.setSystemName(record.getSystemName());
            }
            record.setEditions(list);
        }
        return ResponseData.success(page);
    }

    /**
     * select系统列表
     */
    @ApiOperation("获取select系统列表")
    @GetMapping("/selectlist")
    public ResponseData<List<SqSystemEntity>> selectlist() {
        return ResponseData.success(sqSystemService.list());
    }

    /**
     * 信息
     */
    @ApiOperation("通过系统id获取信息")
    @GetMapping("/info/{id}")
    public ResponseData<SqSystemEntity> info(@PathVariable("id") Long id) {
        SqSystemEntity sqSystem = sqSystemService.getById(id);
        return ResponseData.success(sqSystem);
    }

    /**
     * 保存
     */
    @ApiOperation("新增系统")
    @PostMapping("/save")
    public ResponseData save(@RequestBody SqSystemEntity sqSystem) {
        try {
            if (sqSystem.getAllow() == null) {
                sqSystem.setAllow(1); // 默认允许创建授权码
            }
            sqSystem.setCreateTime(new Date());
            boolean flag = sqSystemService.save(sqSystem);
            return ResponseData.success(flag ? "创建成功" : "创建失败", flag);
        } catch (Exception e) {
            log.error("新增系统接口异常" + Arrays.toString(e.getStackTrace()));
            return ResponseData.error(-1, "系统名称不能重复");
        }
    }

    /**
     * 修改
     */
    @Transactional
    @ApiOperation("修改系统")
    @PostMapping("/update")
    public ResponseData update(@RequestBody SqSystemEntity sqSystem) {
        try {
            boolean flag = sqSystemService.updateById(sqSystem);
            return ResponseData.success(flag ? "修改成功" : "修改失败", flag);
        } catch (Exception e) {
            log.error("修改系统接口异常" + Arrays.toString(e.getStackTrace()));
            return ResponseData.error(-1, "系统名称不能重复");
        }
    }

    /**
     * 删除
     */
    @Transactional
    @ApiOperation("删除系统（单个+多个）")
    @PostMapping("/delete")
    public ResponseData delete(@RequestBody List<Long> ids) {
        boolean flag = sqSystemService.removeByIds(ids);
        // sqSystemAuthorizationCodeService.remove(new QueryWrapper<SqSystemAuthorizationCodeEntity>().in("system_id", ids));
        return ResponseData.success(flag ? "删除成功" : "删除失败", flag);
    }
}
